import Fields from './wt-model-field';
import Utils from './wt-model-utils';
import TimeUtils from './wt-time-utils';
import request from './axios';

function requestOnModelConfig(config, data, successCallback, errorCallback) {
	request(config.url, config.method, data, config.transfer, successCallback, errorCallback);
}

function formatFieldValue(field, value) {
	if (field.type == Fields.types.date) {
		return TimeUtils.format(value);
	}
	else if (field.type == Fields.types.number && field.multiplier) {
		return value * field.multiplier;
	}
	else if (field.type == Fields.types.images) {
		if (field.saveAsJson) {
			return JSON.stringify(value);
		}
		else {
			return value;
		}
	}
	else {
		return value;
	}
}

export default {
	query: function(model, data, successCallback, errorCallback) {
		let config = model.query;
		requestOnModelConfig(config, data, successCallback, errorCallback);
	},
	/** 
	 * 获取单条数据记录
	 * @param {Object} model
	 * @param {Object} id
	 * @param {Object} successCallback
	 * @param {Object} errorCallback
	 */
	getOne: function(model, id, successCallback, errorCallback) {
		let config = model.query;
		let url = null;
		let data = null;
		if (config.method == 'GET') {		
			url = config.url + '/' + id;	
		}
		else {
			url = config.url;
			data = {};
			if (model.id_field) {
				data[model.id_field.name] = id;
			}
			if (config.method == 'DRONE') {
				data['user_id'] = Permissions.getCurrentUserId();
			}
		}
		request(url, config.method, data, config.transfer, successCallback, errorCallback);
	},
	remove: function(model, id, successCallback, errorCallback) {
		let config = model.delete;
		let form = {};
		form[model.id_field.name] = id;
		requestOnModelConfig(config, form, successCallback, errorCallback);
	},
	save: function(model, id, data, successCallback, errorCallback) {		
		let config = null;
		let form = {};
		let isNew = id == null || id === undefined || id == '';
		if (isNew) {
			config = model.new;
		}
		else {
			config = model.update;
			form[model.id_field.name] = id;
		}

		// Build submit form.
		for (let i = 0; i < model.fields.length; i ++) {
			let field = model.fields[i];
			if (field.noSave) {
				continue;
			}
			let name = field.name;
			let value = null;
			if (data[name]) {
				value = data[name];
			}
			else {
				if (data[field.tableName]) {
					if (name === undefined && !field.uploadByComponent) {
						console.warn('Field \'' + field.tableName + '\' is in the data form, but no value name defined.');
					}
					value = data[field.tableName];
				}
				else if (field.rules && field.rules.length > 0 && field.rules[0].required && !field.customComponent) {
					console.error('Unable to find required field \'' + name + '\' (tableName: \'' + field.tableName + '\'!');
					errorCallback('missing field');
					continue;
				}
			}
			form[name] = formatFieldValue(field, value);
		}
		// Fill the extra params.
		if (config.extraParams) {
			for (let i = 0; i < config.extraParams.length; i ++) {
				form[config.extraParams[i]] = data[config.extraParams[i]];
			}
		}

		requestOnModelConfig(config, form, successCallback, errorCallback);
	},
}